package org.mantenimientojsf.generales;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
*
 * @author
*/
public class DataGManager implements Serializable {
private static final long serialVersionUID = 1L;
public static Exception excepcion;
public static Connection con = null;
public DataGManager() {
 try {
    } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "DataGManager()");
  }
}

/*
 WithOutTransaction()
*/

public boolean WithOutTransaction() {
 try {
     con.setAutoCommit(true);
     return true;
    } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "WithOutTransaction()");
  }
    return false;
 }

/*
 BeginTransaction()
*/

public boolean BeginTransaction() {
 try {
     con.setAutoCommit(false);
     return true;
    } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "BeginTransaction()");
  }
    return false;
 }

/*
 Commit()
*/

public boolean Commit() {
 try {
     con.commit();
     return true;
    } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "Commit()");
  }
    return false;
 }

/*
 Roolback()
*/

public boolean Roolback() {
 try {
     con.rollback();
     return true;
    } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "Roolback()");
  }
    return false;
 }

/*
 EndTransaction()
*/

public boolean EndTransaction() {
 try {
     con.setAutoCommit(true);
     return true;
    } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "EndTransaction()");
  }
    return false;
 }

/*
 OpenConnection()
*/



public static boolean OpenConnection() {
try {
  Context ctx = new InitialContext();
  if (ctx == null) {
     JSFUtil.addSuccessMessage("No initial context");
     return false;
  }
   DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mantenimiento");
   if (ds == null) {
      JSFUtil.addSuccessMessage("No data source");
      return false;
   }
   con = ds.getConnection();
   if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
   }
   return true;
  } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex, "Error");
 }
    return false;
 }

/*
 Connect()
*/

public Connection Connect() {
 try {
    if (con == null) {
       return null;
    }
       return con;
  } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "Connect()");
  }
    return null;
 }

/*
 Connect()
*/

 public boolean CloseConnection() {
 try {
 
  if (con == null) {
  return true;
 }
 con.close();
 return true;
 } catch (Exception ex) {
     JSFUtil.addErrorMessage(ex,  "CloseConnection()");
 }
 return false;
 }

/*
 getCon()
*/

 public static Connection getCon() {
    return con;
 }
} 
